﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace xyw.admincp
{
    public partial class UsersClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                InitData();
                BindData();
            }
        }

        private void InitData()
        {
            ddl_College.Items.Clear();
            ddl_College.Items.Add("信息学院");
            ddl_College.Items.Add("人文学院");
            ddl_College.Items.Add("外国语学院");
            ddl_College.Items.Add("管理学院");
            ddl_College.Items.Add("艺术学院");
            ddl_College.Items.Add("现代服务业学院");
            ddl_College.Items.Add("生环学院");
            ddl_College.Items.Add("城建学院");
            ddl_College.Items.Add("继续教育学院");

            ddl_Year.Items.Clear();
            
            for (int i = 1985; i < DateTime.Now.Year; i++)
            {
                ListItem li = new ListItem(i.ToString()+"年",i.ToString());
                
                ddl_Year.Items.Insert(0,li);
            }
        }
        XYW.BLL.Classes bllClass = new XYW.BLL.Classes();

        private void BindData()
        {
            BindRepeaterData(Repeater1, "信息学院");
            BindRepeaterData(Repeater2, "人文学院");
            BindRepeaterData(Repeater3, "外国语学院");
            BindRepeaterData(Repeater4, "管理学院");
            BindRepeaterData(Repeater5, "艺术学院");
            BindRepeaterData(Repeater6, "现代服务业学院");
            BindRepeaterData(Repeater7, "生环学院");
            BindRepeaterData(Repeater8, "城建学院");
            BindRepeaterData(Repeater9, "继续教育学院");
        }

        protected void BindRepeaterData(Repeater re, string collegeName)
        {
            re.DataSource = bllClass.GetList(" collegeName='"+collegeName+"'");
            re.DataBind();
            foreach (RepeaterItem ri in (re.Items))
            {
                (ri.FindControl("btn_Delete") as Button).Text = (ri.FindControl("hd_Name") as HiddenField).Value;
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            XYW.BLL.Classes bllclass = new XYW.BLL.Classes();
            if (e.CommandName == "Delete")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                bllclass.Delete(id);
                BindData();
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            XYW.BLL.Classes bllclass = new XYW.BLL.Classes();
            XYW.Model.Classes modelclass = new XYW.Model.Classes();
            modelclass.className = this.txt_ClassName.Text.Trim();
            modelclass.collegeName = ddl_College.SelectedValue;
            modelclass.leader = "无";
            modelclass.year = int.Parse(ddl_Year.SelectedValue);
            bllclass.Add(modelclass);
            BindData();
        }
    }
}